﻿<%@ Assembly Name="SharePointRsync, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6ef6cf8f9143533e" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="SharePointRsync" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Page Language="C#" %>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Manual RSync
</asp:Content>
<asp:Content ID="Content14" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
    This page is used by adminstrators to manually sync SharePoint content
</asp:Content>
<asp:Content ID="Content15" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/c#" runat="server">
    
        private string _itemid, _listid, _weburl;
        private bool _forceOverwrite = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
            {
                _itemid = Request.QueryString["itemid"].ToString();
            }
            if (Request.QueryString["listid"] != null)
            {
                _listid = Request.QueryString["listid"].ToString();
            }
            if (Request.QueryString["weburl"] != null)
            {
                _weburl = Request.QueryString["weburl"].ToString();
            }
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            string strUrl = "/_catalogs/masterpage/seattle.master";
            MasterPageFile = strUrl;
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (SPSite site = new SPSite(_weburl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string jobName = "Manual file sync for " + web.Title;
                        SyncList.deleteJob(web, jobName);
                        SharePointRsyncJob spsyncjob;

                        if (_itemid == null)
                        {
                            spsyncjob = new SharePointRsyncJob(jobName, SPFarm.Local.TimerService, web.Url, _listid, _forceOverwrite);
                        }
                        else
                        {
                            spsyncjob = new SharePointRsyncJob(jobName, SPFarm.Local.TimerService, web.Url, _listid, _itemid, _forceOverwrite);
                        }

                        SPOneTimeSchedule schedule = new SPOneTimeSchedule();
                        schedule.Time = DateTime.Now.AddMinutes(1);
                        spsyncjob.Schedule = schedule;
                        spsyncjob.Title = jobName;

                        spsyncjob.Update();
                    }
                }

                SubmitButton.Visible = false;
                instructions.Visible = false;
                ForceOverwrite.Visible = false;
                StatusLabel.Text = "<span style='color:blue'>Your job will run within the next minute.</span>";
            }
            catch (Exception ex)
            {
                SubmitButton.Visible = false;
                instructions.Visible = false;
                ForceOverwrite.Visible = false;
                StatusLabel.Text = ex.Message;
            }
        }

        private static void switchUser(SPWeb web, string siteStr, string user)
        {
            //impersonate somebody else
            SPUserToken userToken = web.AllUsers[user].UserToken;
            SPSite s = new SPSite(siteStr, userToken);
            SPWeb w = s.OpenWeb();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(_weburl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Guid listGuid = new Guid(_listid);
                    string listURL = web.Lists[listGuid].DefaultViewUrl;
                    Response.Redirect(listURL);
                }
            }
        }

        protected void ForceOverwrite_Click(object sender, EventArgs e)
        {
            if (ForceOverwrite.Checked == true)
            {
                _forceOverwrite = true;
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content16" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <table cellspacing="0" border="0" width="100%">
        <tr>
            <td>
                <h2 style="margin-left: 10px; margin-bottom: 0px;">Manual Sync Control (you must log in as farm account)
                </h2>
            </td>
        </tr>
        <tr>
            <td>
                <table width="100%" cellpadding="0" cellspacing="0" style="padding: 5px 10px 10px 10px;">
                    <tr>
                        <td valign="top" width="100%">
                            <div>
                                <br />
                                <asp:Label runat="server" ID="instructions">
                                <p style="font-size: 14pt;width:60%">
                                    Depending on the amount of items you are syncing this could take a very long time.
                                    Are you sure you want to continue?</p>
                                </asp:Label>
                                <asp:CheckBox runat="server" ID="ForceOverwrite" OnCheckedChanged="ForceOverwrite_Click"
                                    Text="Force overwrite (will not check for changes but will delete missing files)" />
                                <p style="font-size: 14pt">
                                    <asp:Label runat="server" ID="StatusLabel" />
                                </p>
                                <asp:Button runat="server" ID="SubmitButton" Text="Start Sync" OnClick="SubmitButton_Click" /><asp:Button
                                    runat="server" ID="GoBackButton" Text="Go Back" OnClick="Cancel_Click" />
                                <br />
                            </div>
                        </td>
                        <td>&nbsp;
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</asp:Content>
